import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BioServer {
    //telnet localhost 8000   ctrl+]  send 字符串
    public static void main(String[] args) throws IOException {
        ServerSocket server = null;
        Socket socket = null;

        InputStream inputStream = null;
        OutputStream outputStream = null;
        try{
            server = new ServerSocket(8000);
            while (true){
                //进入阻塞状态，直到连接
                socket = server.accept();  //阻塞，三次握手
                inputStream = socket.getInputStream();
                int len = 0;
                byte[] req = new byte[1024];
                while((len=inputStream.read(req))>0){
                    System.out.println(new String(req,0,len));

                    //业务处理
                    outputStream = socket.getOutputStream();
                    outputStream.write("res".getBytes());
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(server!=null){
                server.close();
            }
        }
    }

}
